package com.wondertek.service.sdk.iotdh.util;

import cn.hutool.core.util.ObjectUtil;
import com.wondertek.common.util.base.StringUtil;
import com.wondertek.common.exception.AppException;
import com.wondertek.common.constant.LanguageCodeStatus;
import com.wondertek.service.sdk.iotdh.service.DhSdkService;
import com.wondertek.service.sdk.iotdh.config.SdkConfig;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class DeviceUtil {

    @Resource
    private SdkConfig sdkConfig;

    @Resource
    private SdkUtil sdkUtil;

    @Resource
    private DhSdkService dhSdkService;

    /**
     * 获取统一请求头
     */
    public HttpHeaders getCommonHeaders(String token){
        long time = System.currentTimeMillis();
        String nonce = StringUtil.getUUId().substring(0, 8);
        String sign = sdkUtil.createSign(time,nonce,sdkConfig);
        if(ObjectUtil.isEmpty(sign)){
            throw new AppException("sign.error");
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type","application/json");
        headers.add("clientId",sdkConfig.getClientId());
        headers.add("authorization",token);
        headers.add("timestamp",time + "");
        headers.add("nonce",nonce);
        headers.add("sign",sign);
        return headers;
    }

    public String getToken(){
        return dhSdkService.getToken();
    }
}
